<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\User;
use App\Models\Role;
use App\Models\Department;
use App\Models\Action;

class LoginController extends Controller
{

    //商户登录
    public function login(Request $request) 
    {

        $login    = $request->input('login');
        $password = $request->input('password');

        if ($login == null || $password == null) {

            return self::error('号和密码不能为空');
        }

        $adminObj = Admin::where('login', $login)->where('status', Admin::STATUS_BEGIN)->first();

        $password = User::create_pwd($password);
        
        if (!empty($adminObj) && $adminObj->password == $password) {

            session(['admin_id' => $adminObj->id]);
            session(['admin_level' => $adminObj->level]);
            //判断是否为超级管理员
            //查询出权限并存入缓存
            $adminObj->level == 1 || $this->getActionSiion($adminObj->roleids, $adminObj->department);

            return self::success('登录成功');
        } else {
            return self::error('帐号或密码错误');
        }
    }

    //存储权限
    private function  getActionSiion ($roleids, $departmentid) 
    {
        if ($departmentid == 0 || empty($roleids)) {
            return session(['action_arr'=> '']);
        }

        //获得部门角色id
        $depart = Department::find($departmentid);
        if (empty($depart) ) {
            return session(['action_arr'=>'']);
        }

        //比对用户角色id 和部门角色id 获得公共部分id 并查询出对应的权限
        
        $depart_role_arr  = explode(',', $depart->roleids);
        $admin_role_arr   = explode(',', $roleids);
        //获得公共角色
        $roles_common_arr = array_intersect($depart_role_arr, $admin_role_arr);

        //过滤启用角色
        $role_obj = Role::whereIn('id', $roles_common_arr)->where('status', Role::STATUS_BEGIN)->get();
        
        if (empty($role_obj)) {
            return session(['action_arr'=> '']);
        }

        $role_arr = [];

        foreach ($role_obj as $value) {
            $role_arr[] = $value->actionids;
        }

        //处理 并查询出 相应的权限
        $roles_str = implode(',', $role_arr);
        $roles_arr = explode(',', $roles_str);

        $action    = Action::whereIn('id', $roles_arr)->where('status', Action::STATUS_BEGIN)->get();

        $action_arr = [];
        $menu       = [];
        foreach ($action as $key => $value) {
            $action_arr[]   = $value->module.'/'.$value->controller.'/'.$value->action;
            if ($value->display == 1) {
                $menu[$key]['id']    = $value->id;
                $menu[$key]['name']  = $value->name;
                $menu[$key]['formation'] = $value->formation;
                $menu[$key]['url']   = $value->url;
                $menu[$key]['pid']   = $value->pid;
            }  
        }
        //存储权限
        session(['action_arr' => $action_arr]);
        //存储菜单
        session(['admin_menu' => $menu]);
    }

    /**
     * 退出登录
     */
    public function out() {

        session(['admin_id' => null]);
        session(['action_arr' => null]);
        return self::success('退出成功');
    }
}

